import todoLocalStorage from "../../utils/todo";
export default {
  namespaced: true,
  state: {
    list: todoLocalStorage.get(),
    dbIpt: "",
  },
  mutations: {
    ADD_LIST(state, data) {
      state.list.push(data);
    },
    UPDETE_LIST(state, { item, index }) {
      item.isIptShow = true;
      state.dbIpt = item.text;
    },
    UPDATE_TODO(state, { item, index }) {
      state.list[index].text = item.text;
      state.list[index].isIptShow = false;
    },
    ESC_TODO(state, { item, index }) {
      state.list[index].text = state.dbIpt;
      state.list[index].isIptShow = false;
    },
    CHANGE_CHECK(state, { item, index }) {
      state.list[index].isCheck = !state.list[index].isCheck;
    },
    CHECK_ALL(state, status) {
      state.list.filter((v) => {
        return (v.isCheck = status);
      });
    },
    DELETE_TODO(state, { item, index }) {
      state.list.splice(index, 1);
    },
    // ?????
    FILTER_TODO(state, status) {
      let arr = todoLocalStorage.get();
      if (status == "all") {
        state.list = arr;
      } else if (status == "active") {
        let activeArr = arr.filter((v) => {
          return v.isCheck == false;
        });
        state.list = activeArr;
      } else {
        let trueArr = arr.filter((v) => {
          return v.isCheck == true;
        });
        state.list = trueArr;
      }
    },
    CLEAR_ALL(state) {
      state.list = state.list.filter((v) => {
        return v.isCheck == false;
      });
    },
  },
  actions: {
    //新增
    addToList({ commit, dispatch }, data) {
      commit("ADD_LIST", data);
      dispatch("localTodoList");
    },
    //修改
    dbTodoClk({ commit, dispatch }, { item, index }) {
      commit("UPDETE_LIST", { item, index });
      dispatch("localTodoList");
    },
    //修改赋值
    xgClk({ commit, dispatch }, { item, index }) {
      commit("UPDATE_TODO", { item, index });
      dispatch("localTodoList");
    },
    //esc
    escClk({ commit, dispatch }, { item, index }) {
      commit("ESC_TODO", { item, index });
      dispatch("localTodoList");
    },
    //失去焦点
    blurChange({ commit, dispatch }, { item, index }) {
      commit("UPDATE_TODO", { item, index });
      dispatch("localTodoList");
    },
    //中横线
    checkChange({ commit, dispatch }, { item, index }) {
      commit("CHANGE_CHECK", { item, index });
      dispatch("localTodoList");
    },
    //all中横线
    checkAllClk({ commit, dispatch }, status) {
      commit("CHECK_ALL", status);
      dispatch("localTodoList");
    },
    //删除
    deleteClk({ commit, dispatch }, { item, index }) {
      commit("DELETE_TODO", { item, index });
      dispatch("localTodoList");
    },
    //btn
    filterFn({ commit, dispatch }, status) {
      commit("FILTER_TODO", status);
    },
    //删除已完成
    clearAllClk({ commit, dispatch }) {
      commit("CLEAR_ALL");
      dispatch("localTodoList");
    },
    //本地
    localTodoList({ state }) {
      todoLocalStorage.set(state.list);
    },
  },
  getters: {
    num(state) {
      return state.list.filter((v) => {
        return v.isCheck == false;
      }).length;
    },
  },
};
